package org.example.completed.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.completed.domain.Post;
import org.example.completed.domain.User;
import org.example.completed.mapper.PostMapper;
import org.example.completed.model.dto.PostAndAvatarDTO;
import org.example.completed.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 帖子表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2025-04-15
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements PostService {
    @Autowired
    private PostMapper postMapper;
    @Autowired
    UserServiceImpl userService;

    @Override
    public boolean addPost(Post post) {
        User user = userService.getById(post.getUserId());
        Post newPost = new Post();
        newPost.setContent(post.getContent());
        newPost.setImageUrl(post.getImageUrl());
        newPost.setUserId(post.getUserId());
        newPost.setAvatarUrl(user.getImg());
        newPost.setUserAcoount(user.getUserAcoount());

        return this.save(newPost);
    }

    @Override
    public PostAndAvatarDTO getPostsByUserId(String userId) {
        LambdaQueryWrapper<Post> wrapper = new LambdaQueryWrapper<Post>()
                .eq(Post::getUserId,userId);
        List<Post> posts = postMapper.selectList(wrapper);

        String img = userService.getImgByUserId(userId);

        PostAndAvatarDTO postAndAvatarDTO = new PostAndAvatarDTO();
        postAndAvatarDTO.setPostList(posts);
        postAndAvatarDTO.setImg(img);

        return postAndAvatarDTO;
    }
}
